package design_patterns;

//策略模式
public class StrategyPattern {
    public static void main(String[] args) {
        Context context;
        //实例化的策略不同，最终的结果也不同
        context = new Context(new ConcreteStrategyA());
        context.contextInterface();
        context = new Context(new ConcreteStrategyB());
        context.contextInterface();
        context = new Context(new ConcreteStrategyC());
        context.contextInterface();
    }
}

/**
 * 抽象算法类
 */
abstract class Strategy{
    public abstract void algorithmInterface();
}
class ConcreteStrategyA extends Strategy {

    @Override
    public void algorithmInterface() {
        System.out.println("算法A的实现");
    }
}
class ConcreteStrategyB extends Strategy {

    @Override
    public void algorithmInterface() {
        System.out.println("算法B的实现");
    }
}
class ConcreteStrategyC extends Strategy {

    @Override
    public void algorithmInterface() {
        System.out.println("算法C的实现");
    }
}

/**
 * 上下文
 */
class Context {
    Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void contextInterface() {
        strategy.algorithmInterface();
    }
}